package smartlifev30.com.sdk.api.service_app;

import android.support.annotation.NonNull;

import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.bean.request.ChangePwdRequest;
import smartlifev30.com.sdk.bean.request.LogOutRequest;
import smartlifev30.com.sdk.bean.request.LoginRequest;
import smartlifev30.com.sdk.bean.request.RegisterCheckRequest;
import smartlifev30.com.sdk.bean.request.ResetPwdRequest;
import smartlifev30.com.sdk.bean.request.SmsCodeRequest;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.baiwei.ResponseHelper;

/**
 * @author tys
 * @date 2018/8/12
 */
public class UserMgmtImpl implements IUserMgmt {

    @Override
    public void loginByPwd(@NonNull String userName, @NonNull String pwd, @NonNull String channelID, @NonNull String appId, @NonNull CallBack<LoginResponse> callback) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setMsg_id(callback.getMsgId());
        loginRequest.setFrom(userName);
        loginRequest.setMsg_class("app_auth");
        loginRequest.setMsg_name("user_login");
        loginRequest.setMsg_type("get");
        LoginRequest.User user = loginRequest.new User();
        user.setUser_pwd(pwd);
        user.setChannelID(channelID);
        user.setAppId(appId);
        loginRequest.setUser(user);
        ResponseHelper.addListen(callback);
        NetManager.getInstance().sendMessage(JsonUtil.toString(loginRequest));
    }

    @Override
    public void loginByToken(@NonNull String userName, @NonNull String token, @NonNull CallBack<LoginResponse> callback) {
        LoginRequest loginRequest = new LoginRequest();
        loginRequest.setMsg_id(callback.getMsgId());
        loginRequest.setFrom(userName);
        loginRequest.setMsg_class("app_auth");
        loginRequest.setMsg_name("token_login");
        loginRequest.setMsg_type("get");
        loginRequest.setToken(token);
        ResponseHelper.addListen(callback);
        NetManager.getInstance().sendMessage(JsonUtil.toString(loginRequest));
    }

    @Override
    public void logOut(@NonNull String userName, @NonNull String token, @NonNull CallBack<LoginResponse> callback) {
        LogOutRequest logOutRequest = new LogOutRequest();
        logOutRequest.setMsg_id(callback.getMsgId());
        logOutRequest.setFrom(userName);
        logOutRequest.setMsg_class("app_auth");
        logOutRequest.setMsg_name("token_login");
        logOutRequest.setMsg_type("get");
        logOutRequest.setToken(token);
        ResponseHelper.addListen(callback);
        NetManager.getInstance().sendMessage(JsonUtil.toString(logOutRequest));
    }

    @Override
    public void changePwd(@NonNull String userName, @NonNull String token, @NonNull String oldPwd, @NonNull String newPwd, @NonNull CallBack<LoginResponse> callback) {
        ChangePwdRequest changePwdRequest = new ChangePwdRequest();
        changePwdRequest.setMsg_id(callback.getMsgId());
        changePwdRequest.setFrom(userName);
        changePwdRequest.setMsg_class("app_auth");
        changePwdRequest.setMsg_name("user_change_pwd");
        changePwdRequest.setMsg_type("get");
        changePwdRequest.setToken(token);
        ChangePwdRequest.User user = changePwdRequest.new User(oldPwd, newPwd);
        changePwdRequest.setUser(user);
        ResponseHelper.addListen(callback);
        NetManager.getInstance().sendMessage(JsonUtil.toString(changePwdRequest));
    }

    @Override
    public void regCheck(@NonNull String userName, @NonNull String token, @NonNull String user, @NonNull CallBack<LoginResponse> callback) {
        RegisterCheckRequest registerCheck = new RegisterCheckRequest();
        registerCheck.setMsg_id(callback.getMsgId());
        registerCheck.setFrom(userName);
        registerCheck.setMsg_class("app_auth");
        registerCheck.setMsg_name("user_regcheck");
        registerCheck.setMsg_type("get");
        registerCheck.setToken(token);
        RegisterCheckRequest.User u = registerCheck.new User(user);
        registerCheck.setUser(u);
        ResponseHelper.addListen(callback);
        NetManager.getInstance().sendMessage(JsonUtil.toString(registerCheck));
    }

    @Override
    public void smsCode(@NonNull String userName, @NonNull Integer type, @NonNull CallBack<LoginResponse> callback) {
        SmsCodeRequest smsCodeRequest = new SmsCodeRequest();
        smsCodeRequest.setMsg_id(callback.getMsgId());
        smsCodeRequest.setFrom(userName);
        smsCodeRequest.setMsg_class("app_auth");
        smsCodeRequest.setMsg_name("smscode");
        smsCodeRequest.setMsg_type("get");
        SmsCodeRequest.Sms sms = smsCodeRequest.new Sms(type);
        smsCodeRequest.setSms(sms);
        ResponseHelper.addListen(callback);
        NetManager.getInstance().sendMessage(JsonUtil.toString(smsCodeRequest));
    }

    @Override
    public void resetPwd(@NonNull String userName, @NonNull String code, @NonNull String newPwd, @NonNull CallBack<LoginResponse> callback) {
        ResetPwdRequest resetPwdRequest = new ResetPwdRequest();
        resetPwdRequest.setMsg_id(callback.getMsgId());
        resetPwdRequest.setFrom(userName);
        resetPwdRequest.setMsg_class("app_auth");
        resetPwdRequest.setMsg_name("reset_pwd");
        resetPwdRequest.setMsg_type("get");
        ResetPwdRequest.User user = resetPwdRequest.new User(code, newPwd);
        resetPwdRequest.setUser(user);
        ResponseHelper.addListen(callback);
        NetManager.getInstance().sendMessage(JsonUtil.toString(resetPwdRequest));
    }
}
